package org.grow.pointcolor.widget;

import org.grow.pointcolor.tool.FormatTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@Component
public class DetectiveButton extends JButton
        implements MouseListener,ActionListener {
    @Autowired
    private JFormattedTextField xTextField;
    @Autowired
    private JFormattedTextField yTextField;
    @Autowired
    private JFormattedTextField outTextField;
    @Autowired
    private Robot robot;
    @Autowired
    private Console console;

    public DetectiveButton() {
        super("键盘k");
    }

    @PostConstruct
    public void springInit(){
        this.addMouseListener(this);
//        this.addActionListener(this);
        this.registerKeyboardAction(
                this,KeyStroke.getKeyStroke('k'),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        int x = Integer.parseInt(xTextField.getText());
        int y = Integer.parseInt(yTextField.getText());
        Color c= robot.getPixelColor(x,y);
        String s = FormatTool.pointColorFormat(x, y, c);
        outTextField.setText(s);
        console.append("]]"+s+System.lineSeparator());
        outTextField.setBackground(c);


    }



    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Point location = MouseInfo.getPointerInfo().getLocation();
        xTextField.setText(location.x + "");
        yTextField.setText(location.y + "");
        Color c = robot.getPixelColor(location.x, location.y);
        String s = FormatTool.pointColorFormat(location.x, location.y, c);
        outTextField.setText(s);
        console.append("[["+s+System.lineSeparator());
        outTextField.setBackground(c);
    }
}
